echo $'\n'"=======  executing sqlImport.sh =============== "$'\n'

source $WORKSPACE/jenkins/config

echo "dir is `pwd`"

#查询执行过的sql
mysql -u $db_username -p$db_password -h $dbIp --default-character-set=utf8 $schema < jenkins/shell/executedSqls.sql > /tmp/executedSqls

if [ ! "$?"  -eq 0 ]; then
    echo "fail to get executedSqls"
    exit
fi

#格式化成字符串，用于后续判断
executedSqls=`sed -i ':a;N;s/\n/,/g;ta' /tmp/executedSqls && cat /tmp/executedSqls`

echo "executedSqls is  [$executedSqls]"

if [ ! -d "$sqlDir" ]; then
    echo "sqlDir[$sqlDir] not exist, abort"
    exit
fi

for i in `find $sqlDir -name "*.sql" | sort`
do
	if [[ "$executedSqls" =~ "$i" ]] #是否包含
	then
		echo [$i] has been executed before,skip.
	else
	  echo RUNNING [$i] ---------
    mysql -u $db_username -p$db_password -h $dbIp --default-character-set=utf8 $schema < "$i"  && #执行sql
		mysql -u $db_username -p$db_password -h $dbIp --default-character-set=utf8 $schema -e "insert into sql_executed_log (file) values ('$i')" && #执行记录
		echo $i executes successful
	fi
done
